Windows PowerShell Cookbook 2013 by Unknown
Author:Unknown
Language: eng
Format: epub
Discussion
You can access many simple Windows APIs using the script given in Recipe 17.5 . This approach is difficult for more complex APIs, however.
In PowerShell version one, it was possible to access these APIs in one of two ways: by generating a dynamic assembly on the fly (you wouldn’t really do this for one-off calls, but Recipe 17.5 uses this technique) or by looking up the P/Invoke definition for that API call and compiling the C# to access it.
These are both good approaches, but PowerShell version two introduces the Add-Type cmdlet to make this much easier.
Add-Type offers four basic modes of operation:
PS > Get-Command Add-Type | Select -Expand ParameterSets | Select Name
----
FromSource
FromMember
FromPath
FromAssemblyName
These modes of operation are:
FromSource
Compile some C# (or other language) code that completely defines a type. This is useful when you want to define an entire class, its methods, namespace, etc. You supply the actual code as the value to the -TypeDefinition parameter, usually through a variable. For more information about this technique, see Recipe 17.6 .
FromPath
Compile from a file on disk, or load the types from an assembly at that location. For more information about this technique, see Recipe 17.8 .
FromAssemblyName
Load an assembly from the .NET Global Assembly Cache (GAC) by its shorter name. This is not the same as the [Reflection.Assembly]::LoadWithPartialName method, since that method introduces your script to many subtle breaking changes. Instead, PowerShell maintains a large mapping table that converts the shorter name you type into a strongly named assembly reference. For more information about this technique, see Recipe 17.8 .
FromMember
Generates a type out of a member definition (or a set of them). For example, if you specify only a method definition, PowerShell automatically generates the wrapper class for you. This parameter set is explicitly designed to easily support P/Invoke calls.
Now, how do you use the FromMember parameter set to call a Windows API? The solution shows the end result of this process, but let’s take it step-by-step. First, imagine that you want to access sections of an INI file.
PowerShell doesn’t have a native way to manage INI files, and neither does the .NET Framework. However, the Windows API does, through a call to the function called GetPrivateProfileString . The .NET framework lets you access Windows functions through a technique called P/Invoke (Platform Invocation Services). Most calls boil down to a simple P/Invoke definition , which usually takes a lot of trial and error. How-ever, a great community has grown around these definitions, resulting in an enormous resource called P/Invoke .NET ( http://www.pinvoke.net/ ). The .NET Framework team also supports a tool called the P/Invoke Interop Assistant that generates these defini-tions as well, but we won’t consider that for now.
First, we’ll create a script called Get-PrivateProfileString.ps1 . It’s a template for now: ## Get-PrivateProfileString.ps1
param(
$Path,
$Category,
$Key)
$null
To start fleshing this out, we visit P/Invoke .NET and search for GetPrivateProfile String , as shown in Figure 17-1 .
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26810)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(20676)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20451)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20448)
The Fifty Shades Trilogy & Grey by E L James(19317)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19268)
Shot Through the Heart by Mercy Celeste(19133)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17299)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17232)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(17080)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(17003)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16619)
The Subtle Art of Not Giving a F*ck by Mark Manson(14622)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14317)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13981)
Scorched Earth by Nick Kyme(12968)
Drei Generationen auf dem Jakobsweg by Stein Pia(11151)
Suna by Ziefle Pia(11079)
Scythe by Neal Shusterman(10560)